from lxml import etree
import re
import math
    
########招标预警网表格获取/广东省政府采购中心##################
"""
所有竖表格都是如下思路
获取表格源码
获取表头
获取表格数据总数
表格数据总数/表头获取行数
获取每行数据
根据表头获取相关数据

横表：
获取每行数据
根据关键词获取其所在位置的下一个位置的数据为其对应数据

横竖表判定
除了第一行，其余行是否含有关键词
竖表就用关键词所在位置的下一个数据作为标签的结果

不同的函数是因为表格源码中td, th等区别
"""

def get_table_td_new(res, table_dict):
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    try:
        cut_res = re.findall('<table.*?/table>', res, re.S)
        if cut_res[0].count('<table') > 1:
            for i in range(cut_res[0].count('<table')):
                if i  > 0:
                    re_doc = '<table.*?/table>' + '.*?/table>'
            cut_res = re.findall(re_doc, res, re.S)
        del_str_list = re.findall('<.*?>', cut_res[0])
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str:
                del_list.append(d_str)
        del_res = cut_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
        new_res = etree.HTML(del_res)
        h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
        table = new_res.xpath('//table/tbody/tr/td/text()')
#        print (del_res)
#        print ('td', h_tab, table)
        line_num = math.ceil(len(table)/len(h_tab))
#        print (line_num)
        result_dict = {}
        for i in range(int(line_num)):
            n = i+1
            line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n))
            line_res = [x.replace('\t', '') for x in line_res]
            line_res = [x.replace('\n', '') for x in line_res]
            line_res = [x.replace('\r', '') for x in line_res]
            line_res = [x.replace('\\t', '') for x in line_res]
            line_res = [x.replace('\\n', '') for x in line_res]
            line_res = [x.replace('\\r', '') for x in line_res]
            line_res = [x.replace('\xa0', '') for x in line_res]
            line_res = [x.replace(' ', '') for x in line_res]
            result_dict[n] = line_res
        if len(result_dict[2]) != len(result_dict[1]):
            x = result_dict[1][len(result_dict[1])]
#        print (result_dict)
        H_table_stat = False
        for lab_word_list in table_dict.values():
            for line, value_list in result_dict.items():
                if line != 1 and len(value_list) < 10:
                    for word in lab_word_list:
                        if word in value_list:
                            H_table_stat = True
        
        table_result = {}
#        print (H_table_stat)
        if H_table_stat:
            for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in result_dict.values():
                            if word in tab_val and "排序" not in tab_val:
                                val_ind = tab_val.index(word) + 1
                                if val_ind <= len(tab_val):
                                    if lab == "中标金额" or lab =="预算":
#                                        print (tab_val)
                                        if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                            table_result[lab] = tab_val[val_ind]+"万元"
                                        else:
                                            table_result[lab] = tab_val[val_ind]
                                    else:
                                        table_result[lab] = tab_val[val_ind]
                                    break
                        if lab in table_result:
                            break

        else:        
            for lab, word_list in table_dict.items():
                for word in word_list:
                    for tab_val in result_dict[1]:
                        if lab == "项目名称":
                            if word in tab_val and "排序" not in tab_val and "编号" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind]
                                    break 
                        elif lab == "中标供应商":
                            if word in tab_val and "排序" not in tab_val and "地址" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind]
                                    break 
                        else:
                            if word in tab_val and "排序" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind] + "万元"
                                    break
                                else:
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break

    except:
        try:
            cut_res = re.findall('<table.*?/table>', res, re.S)
            num = False
            for i in range(len(cut_res)):
                if '金额' in cut_res[i]:
                    num = i
                    break
            if num == False:
                for i in range(len(cut_res)):
                    if "中标" in cut_res[i]:
                        num = i
                        break
            if num == False:
                for i in range(len(cut_res)):
                    if "项目名称" in cut_res[i] or '包组' in cut_res[i] or '供应商' in cut_res[i]:
                        num = i
                        break
            if cut_res[num].count('<table') > 1:
                for i in range(cut_res[num].count('<table')):
                    if i  > 0:
                        re_doc = '<table.*?/table>' + '.*?/table>'
                cut_res = re.findall(re_doc, res, re.S)
            del_str_list = re.findall('<.*?>', cut_res[num])
            del_list = []
            for d_str in del_str_list:
                if '<table' not in d_str and '<tbody' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str:
                    del_list.append(d_str)
            del_res = cut_res[num]
            for x in del_list:
                del_res = del_res.replace(x + '\n', '')
                del_res = del_res.replace(x, '')
            del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
            del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
            new_res = etree.HTML(del_res)
            h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
            table = new_res.xpath('//table/tbody/tr/td/text()')
            
#            print (h_tab)
            line_num = math.ceil(len(table)/len(h_tab))
#            print (line_num)
            result_dict = {}
            for i in range(int(line_num)):
                n = i+1
                line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n))
                line_res = [x.replace('\t', '') for x in line_res]
                line_res = [x.replace('\n', '') for x in line_res]
                line_res = [x.replace('\r', '') for x in line_res]
                line_res = [x.replace('\\t', '') for x in line_res]
                line_res = [x.replace('\\n', '') for x in line_res]
                line_res = [x.replace('\\r', '') for x in line_res]
                line_res = [x.replace('\xa0', '') for x in line_res]
                line_res = [x.replace(' ', '') for x in line_res]
                result_dict[n] = line_res
#            print ('拿到的表格数据', result_dict)
            H_table_stat = False
            for lab_word_list in table_dict.values():
                for line, value_list in result_dict.items():
                    if line != 1 and len(value_list) < 10:
                        for word in lab_word_list:
                            if word in value_list:
                                H_table_stat = True
            table_result = {}
            if H_table_stat:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in result_dict.values():
                            if word in tab_val and "排序" not in tab_val:
                                val_ind = tab_val.index(word) + 1
                                if val_ind <= len(tab_val):
                                    if lab == "中标金额" or lab =="预算":
#                                        print (tab_val)
                                        if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                            table_result[lab] = tab_val[val_ind]+"万元"
                                        else:
                                            table_result[lab] = tab_val[val_ind]
                                    else:
                                        table_result[lab] = tab_val[val_ind]
                                    break
                        if lab in table_result:
                            break
            else:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in result_dict[1]:
                            if lab == "项目名称":
                                if word in tab_val and "排序" not in tab_val and "编号" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break
                            elif lab == "中标供应商":
                                if word in tab_val and "排序" not in tab_val and "地址" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break 
                            else:
                                if word in tab_val and "排序" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind] + "万元"
                                        break
                                    else:
                                        if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                            table_result[lab] = result_dict[2][ind]
                                            break
                            if lab in table_result:
                                break
                        
        except Exception as e:
#            print ('td_error:' + str(e))
            table_result = {}
#    print ("td_result:", table_result)
    return table_result





#########珠海市公共资源交易中心##############
def get_table_th(res, table_dict):
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    try:
        cut_res = re.findall('<table.*?/table>', res, re.S)
        if cut_res[0].count('<table') > 1:
            for i in range(cut_res[0].count('<table')):
                if i  > 0:
                    re_doc = '<table.*?/table>' + '.*?/table>'
            cut_res = re.findall(re_doc, res, re.S)
        del_str_list = re.findall('<.*?>', cut_res[0])
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<thead' not in d_str and '</thead' not in d_str and '<th' not in d_str and '</th' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str:
                del_list.append(d_str)
        del_res = cut_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
#        del_res = cut_res[0]
#        print (del_res)
        new_res = etree.HTML(del_res)
        if '<thead' not in del_res:
            if '</th> \n      <td' not in del_res and '</th> \n       <td' not in del_res:
                h_tab = new_res.xpath('//table/tbody/tr[1]/th/text()')
                table1 = new_res.xpath('//table/tbody/tr/th/text()')
                table2 = new_res.xpath('//table/tbody/tr/td/text()')
    #            print (h_tab)
    #            print (table1)
    #            print (table2)
                line_num = math.ceil((len(table1)+len(table2))/len(h_tab))
    #            print (line_num)
                result_dict = {}
                for i in range(int(line_num)):
                    n = i+1
                    if n == 1:
                        line_res = new_res.xpath('//table/tbody/tr[%s]/th/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    else:
                        line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n] = line_res
            else:
                result_dict = {}
#                h_tab = new_res.xpath('//table/tbody/tr[1]/th/text()')
                table1 = new_res.xpath('//table/tbody/tr/th/text()')
                table2 = new_res.xpath('//table/tbody/tr/td/text()')
                line_num = len(table1)
                for i in range(line_num):
                    n = i+1
                    line_res = [table1[i], table2[i]]
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n] = line_res
        else:
            h_tab = new_res.xpath('//table/thead/tr[1]/th/text()')
            table1 = new_res.xpath('//table/thead/tr/th/text()')
            table2 = new_res.xpath('//table/tbody/tr/td/text()')
            
#            print (h_tab)
#            print (table1)
#            print (table2)
            line_num = math.ceil((len(table1)+len(table2))/len(h_tab))
#            print (line_num)
            result_dict = {}
            for i in range(int(line_num)):
                n = i+1
                if n == 1:
                    line_res = new_res.xpath('//table/thead/tr[%s]/th/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                else:
                    line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n-1))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                result_dict[n] = line_res
        table_result = {}
        H_table_stat = False
        for lab_word_list in table_dict.values():
            for line, value_list in result_dict.items():
                if line != 1 and len(value_list) < 5:
                    for word in lab_word_list:
                        if word in value_list:
                            H_table_stat = True
#        print (result_dict, H_table_stat)
        if H_table_stat:
            for lab, word_list in table_dict.items():
                for word in word_list:
                    for line,tab_val in result_dict.items():
                        if word in tab_val and "排序" not in tab_val:
                            val_ind = tab_val.index(word) + 1
                            if val_ind <= len(tab_val):
                                if lab == "中标金额" or lab =="预算":
    #                                        print (tab_val)
                                    if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                        table_result[lab] = tab_val[val_ind]+"万元"
                                    else:
                                        table_result[lab] = tab_val[val_ind]
                                else:
                                    table_result[lab] = tab_val[val_ind]
                                break
                    if lab in table_result:
                        break
        else:
            for lab, word_list in table_dict.items():
                for word in word_list:
                    for tab_val in result_dict[1]:
                        if lab == "项目名称":
                            if word in tab_val and "排序" not in tab_val and "编号" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind]
                                    break
                        elif lab == "中标供应商":
                            if word in tab_val and "排序" not in tab_val and "地址" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind]
                                    break 
                        else:
                            if word in tab_val and "排序" not in tab_val:
                                ind = result_dict[1].index(tab_val)
                                if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind] + "万元"
                                    break
                                else:
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break
                        if lab in table_result:
                            break

    except Exception as e:
#        print ('th_error:' + str(e))
        table_result = {}
#    print ('th_result', table_result)
    return table_result


##############深圳市政府采购中心/深圳市公共资源交易平台/广东三方诚信招标有限公司####################
"""
这个网站需要输入表格临近的关键词
"""
def get_table_keyword_new(res, keyword, table_dict):
    print ("关键词表格")
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    try:
        cut_res = re.findall(keyword + '.*?<table.*?/table>', res, re.S)
        if len(cut_res) > 1:
            for k in range(len(cut_res)):
                if "金额" in cut_res[k] or "价" in cut_res[k] or "合计" in cut_res[k] or "结果" in cut_res[k] or "租金" in cut_res[k]:
                    cut_res = [cut_res[k]]
                    break
#        print(cut_res)
#        print ('-'*40)
        n_res = re.findall('<table.*?/table>', cut_res[0], re.S)
#        print (n_res)
        del_str_list = re.findall('<.*?>', n_res[0],re.S)
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<thead' not in d_str and '</thead' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str and '<th' not in d_str and '</th' not in d_str:
                del_list.append(d_str)
        del_res = n_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
#        print (del_list)
        new_res = etree.HTML(del_res)
        try:
            if '<thead' in del_res and del_res.find('<thead') < del_res.find('<tbody'):
                if '<th ' not in del_res and "<th>" not in del_res:
                    h_tab = new_res.xpath('//table/thead/tr[1]/td/text()')
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')    
#                    print (h_tab)
                    line_num = math.ceil(((len(table1))+len(h_tab))/len(h_tab))
    #                print (line_num)
                    result_dict = {}
                    for i in range(int(line_num)):
                        n = i+1
                        if n==1:
                            line_res = new_res.xpath('//table/thead[1]/tr[%s]/td/text()' % str(n))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
                        else:
                            line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n-1))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
                else:
                    h_tab = new_res.xpath('//table/thead/tr[1]/th/text()')
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')    
    #                print (h_tab)
                    line_num = math.ceil(((len(table1))+len(h_tab))/len(h_tab))
    #                print (line_num)
                    result_dict = {}
                    for i in range(int(line_num)):
                        n = i+1
                        if n==1:
                            line_res = new_res.xpath('//table/thead[1]/tr[%s]/th/text()' % str(n))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
                        else:
                            line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n-1))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
            elif "<thead" in del_res and "<tbody" not in del_res:
                h_tab = new_res.xpath('//table/thead/tr[1]/td/text()')
                table1 = new_res.xpath('//table/thead/tr/td/text()')
                line_num = math.ceil(len(table1)/len(h_tab))
                result_dict = {}
                for i in range(int(line_num)):
                    n = i+1
                    line_res = new_res.xpath('//table/thead/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n] = line_res
            else:
                if '<th ' not in del_res and '<th>' not in del_res:
                    h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')  
                    del_stat = False
                    if len(h_tab) == 1:
                        h_tab = new_res.xpath("//table/tbody/tr[2]/td/text()")
                        line_num = math.ceil((len(table1)-1)/len(h_tab))
                        del_stat = True
#                    print (h_tab)
                    else:
                        line_num = math.ceil((len(table1))/len(h_tab))
                    result_dict = {}
#                    print (h_tab, table1)
                    for i in range(int(line_num)):
                        if del_stat:
                            n = i+2
                        else:
                            n = i+1
                        line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                        if del_stat:
                            result_dict[n-1] = line_res
                        else:
                            result_dict[n] = line_res
                else:
                    h_tab = new_res.xpath('//table/tbody/tr[1]/th/text()')
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')    
#                    print (h_tab, table1)
                    line_num = math.ceil((len(table1) + len(h_tab))/len(h_tab))
#                    print (line_num)
                    result_dict = {}
                    for i in range(int(line_num)):
                        n = i+1
                        if n == 1:
                            line_res = new_res.xpath('//table/tbody[1]/tr[%s]/th/text()' % str(n))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
                        else:
                            line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                            line_res = [x.replace('\t', '') for x in line_res]
                            line_res = [x.replace('\n', '') for x in line_res]
                            line_res = [x.replace('\r', '') for x in line_res]
                            line_res = [x.replace('\\t', '') for x in line_res]
                            line_res = [x.replace('\\n', '') for x in line_res]
                            line_res = [x.replace('\\r', '') for x in line_res]
                            line_res = [x.replace('\xa0', '') for x in line_res]
                            line_res = [x.replace(' ', '') for x in line_res]
                            result_dict[n] = line_res
#            print ("table_dict", result_dict)
            H_table_stat = False
            for lab_word_list in table_dict.values():
                for line, value_list in result_dict.items():
                    if line != 1 and len(value_list) < 5:
                        for word in lab_word_list:
                            if word in value_list:
                                H_table_stat = True
                                
            HX_table_status = False
            if len(result_dict[1]) > 2:
                if "候选人" in result_dict[1][1] and "候选人" in result_dict[1][2]:
                    HX_table_status = True
#            print ("table_stat", H_table_stat)
            table_result = {}
            if H_table_stat:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        
                        if HX_table_status:
                                for line, tab_val in result_dict.items():
                                    if line !=1:
                                        if word in tab_val and "排序" not in tab_val:
                                            val_ind = tab_val.index(word) + 1
                                            if val_ind <= len(tab_val):
                                                if lab == "中标金额" or lab =="预算":
                                                    if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                                        table_result[lab] = tab_val[val_ind]+"万元"
                                                        break
                                                    else:
                                                        table_result[lab] = tab_val[val_ind]
                                                        break
                                                else:
                                                    table_result[lab] = tab_val[val_ind]
                                                    break
                                if lab in table_result:
                                    break
                        else:
                            for line,tab_val in result_dict.items():
                                if word in tab_val and "排序" not in tab_val:
                                    val_ind = tab_val.index(word) + 1
                                    if val_ind <= len(tab_val):
                                        if lab == "中标金额" or lab =="预算":
    #                                        print (tab_val)
                                            if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                                table_result[lab] = tab_val[val_ind]+"万元"
                                            else:
                                                table_result[lab] = tab_val[val_ind]
                                        else:
                                            table_result[lab] = tab_val[val_ind]
                                        break
                            if lab in table_result:
                                break
            else:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in result_dict[1]:
                            if lab == "中标金额" or lab =="预算":
                                if word in tab_val and "控制" not in tab_val and "预算" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind] + "万元"
                                    else:
                                        table_result[lab] = result_dict[2][ind]
                                    break
                            elif lab == "项目名称":
                                if word in tab_val and "编号" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    table_result[lab] = result_dict[2][ind]
                                    break
                            elif lab == "中标供应商":
                                if word in tab_val and "排序" not in tab_val and "地址" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break 
                            else:
                                if word in tab_val and "排序" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind] + "万元"
                                    else:
                                        table_result[lab] = result_dict[2][ind]
                                    break
                        if lab in table_result:
                            break
    
        except:
            h_tab = new_res.xpath('//table/tr[1]/td/text()')
            table1 = new_res.xpath('//table/tbody/tr/td/text()')    
    #            print (h_tab)
            line_num = (len(table1))/len(h_tab)
    #            print (line_num)
            result_dict = {}
            for i in range(1):
                n = i+1
                line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                line_res = [x.replace('\t', '') for x in line_res]
                line_res = [x.replace('\n', '') for x in line_res]
                line_res = [x.replace('\r', '') for x in line_res]
                line_res = [x.replace('\\t', '') for x in line_res]
                line_res = [x.replace('\\n', '') for x in line_res]
                line_res = [x.replace('\\r', '') for x in line_res]
                line_res = [x.replace('\xa0', '') for x in line_res]
                line_res = [x.replace(' ', '') for x in line_res]
                result_dict[n] = line_res
#            print (result_dict)
            H_table_stat = False
            for lab_word_list in table_dict.values():
                for line, value_list in result_dict.items():
                    if line != 1 and len(value_list) < 5:
                        for word in lab_word_list:
                            if word in value_list:
                                H_table_stat = True
            table_result = {}
            if H_table_stat:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in result_dict.values():
                            if word in tab_val and "排序" not in tab_val:
                                val_ind = tab_val.index(word) + 1
                                if lab == "中标金额" or lab =="预算":
#                                    print (tab_val)
                                    if "万元" in tab_val[val_ind-1] and "万元" not in tab_val[val_ind]:
                                        table_result[lab] = tab_val[val_ind]+"万元"
                                    else:
                                        table_result[lab] = tab_val[val_ind]
                                else:
                                    table_result[lab] = tab_val[val_ind]
                                break
                        if lab in table_result:
                            break
            else:
                for lab, word_list in table_dict.items():
                    for word in word_list:
                        for tab_val in h_tab:
                            if lab == "中标金额" or lab =="预算":
                                if word in tab_val and "控制" not in tab_val and "预算" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind] + "万元"
                                    else:
                                        table_result[lab] = result_dict[2][ind]
                                    break
                            elif lab == "项目名称":
                                if word in tab_val and "排序" not in tab_val and "编号" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    table_result[lab] = result_dict[2][ind]
                                    break
                            elif lab == "中标供应商":
                                if word in tab_val and "排序" not in tab_val and "地址" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind]
                                        break 
                            else:
                                if word in tab_val and "排序" not in tab_val:
                                    ind = result_dict[1].index(tab_val)
                                    if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                        table_result[lab] = result_dict[2][ind] + "万元"
                                    else:
                                        table_result[lab] = result_dict[2][ind]
                                    break
                        if lab in table_result:
                            break
    
    except Exception as e:
    #        print ('keyword_error:' +str(e))
        table_result = {}
    try:
        if "中标金额" not in table_result and "中标供应商" in table_result:
            money_res = re.findall('<table.*?/table>', res, re.S)
            for i in range(len(money_res)):
                if money_res[i].count('<table') > 1:
                    money_res[i] = money_res[i].replace(re.findall('(<table.*?)<table', money_res[i], re.S)[0], '')
                
            money_num = 'False'
            for i in range(len(money_res)):
                if '报价' in money_res[i] or '金额' in money_res[i]:
                    money_num = i
                    break
            if money_num != 'False':
    #                print ('获取第二表格')
                money_del_str_list = re.findall('<.*?>', money_res[money_num])
                money_del_list = []
                for m_d_str in money_del_str_list:
                    if '<table' not in m_d_str and '<tbody' not in m_d_str and '<tr' not in m_d_str and '<td' not in m_d_str and '</table' not in m_d_str and '</tbody' not in m_d_str and '</tr' not in m_d_str and '</td' not in m_d_str:
                        money_del_list.append(m_d_str)
                money_del_res = money_res[money_num]
                for x in money_del_list:
                    money_del_res = money_del_res.replace(x + '\n', '')
                    money_del_res = money_del_res.replace(x, '')
                new_res = etree.HTML(money_del_res)
                h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
                table = new_res.xpath('//table/tbody/tr/td/text()')
                
    #                print (h_tab)
                line_num = math.ceil(len(table)/len(h_tab))
    #                print (line_num)
                money_result_dict = {}
                for i in range(int(line_num)):
                    n = i+1
                    line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace(' ', '') for x in line_res]
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    money_result_dict[n] = line_res
                target_num = 'False'
                tag_comp = table_result["中标供应商"].replace('  ', '')
                tag_comp = tag_comp.replace(' ', '')
                for ind, re_list in money_result_dict.items():   
                    if tag_comp in re_list:
                        target_num = ind
                        break
                if target_num != 'False':
                    for i in range(len(money_result_dict[1])):
                        if "报价" in money_result_dict[1][i]:
                            if "万元" in money_result_dict[1][i]:
                                table_result["中标金额"] = money_result_dict[target_num][i] + "万元"
                            else:
                                table_result["中标金额"] = money_result_dict[target_num][i]
    except:
        1    
    return table_result

def get_table_keyword_old(res, keyword, table_dict):
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    try:
        cut_res = re.findall(keyword + '<.*?<table.*?/table>', res, re.S)
#        print(cut_res[0])
        n_res = re.findall('<table.*?/table>', cut_res[0], re.S)
        del_str_list = re.findall('<.*?>', n_res[0])
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str:
                del_list.append(d_str)
        del_res = n_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
        new_res = etree.HTML(del_res)
        try:
            h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
            table1 = new_res.xpath('//table/tbody/tr/td/text()')    
#            print (h_tab)
            line_num = math.ceil((len(table1))/len(h_tab))
#            print (line_num)
            result_dict = {}
            for i in range(int(line_num)):
                n = i+1
                line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                line_res = [x.replace('\t', '') for x in line_res]
                line_res = [x.replace('\n', '') for x in line_res]
                line_res = [x.replace('\r', '') for x in line_res]
                line_res = [x.replace('\\t', '') for x in line_res]
                line_res = [x.replace('\\n', '') for x in line_res]
                line_res = [x.replace('\\r', '') for x in line_res]
                line_res = [x.replace('\xa0', '') for x in line_res]
                line_res = [x.replace(' ', '') for x in line_res]
                result_dict[n] = line_res
            table_result = {}
#            print ("result_dict", result_dict)
            for lab, word_list in table_dict.items():
                for word in word_list:
                    for tab_val in result_dict[1]:
                        if word in tab_val and "排序" not in tab_val:
                            ind = result_dict[1].index(tab_val)
                            if "万元" in tab_val and "万元" not in result_dict[2][ind]:
                                table_result[lab] = result_dict[2][ind] + "万元"
                            else:
                                if "：" not in result_dict[2][ind] and ":" not in result_dict[2][ind]:
                                    table_result[lab] = result_dict[2][ind]
                            break

        except:
            h_tab = new_res.xpath('//table/tr[1]/td/text()')
            table1 = new_res.xpath('//table/tbody/tr/td/text()')    
#            print (h_tab)
            line_num = (len(table1))/len(h_tab)
#            print (line_num)
            result_dict = {}
            for i in range(1):
                n = i+1
                line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                line_res = [x.replace('\t', '') for x in line_res]
                line_res = [x.replace('\n', '') for x in line_res]
                line_res = [x.replace('\r', '') for x in line_res]
                line_res = [x.replace('\\t', '') for x in line_res]
                line_res = [x.replace('\\n', '') for x in line_res]
                line_res = [x.replace('\\r', '') for x in line_res]
                line_res = [x.replace('\xa0', '') for x in line_res]
                line_res = [x.replace(' ', '') for x in line_res]
                result_dict[n] = line_res
            table_result = {}
            for lab, word_list in table_dict.items():
                for word in word_list:
                    for tab_val in h_tab:
                        if word in tab_val and "排序" not in tab_val:
                            ind = h_tab.index(tab_val)
                            if "万元" in tab_val and "万元" not in result_dict[1][ind]:
                                table_result[lab] = result_dict[1][ind] + "万元"
                            else:
                                if "：" not in result_dict[1][ind] and ":" not in result_dict[1][ind]:
                                    table_result[lab] = result_dict[1][ind]
                            break
                        if lab in table_result:
                            break

    except Exception as e:
#        print ('keyword_error:' +str(e))
        table_result = {}
    try:
        if "中标金额" not in table_result and "中标供应商" in table_result:
            money_res = re.findall('<table.*?/table>', res, re.S)
            for i in range(len(money_res)):
                if money_res[i].count('<table') > 1:
                    money_res[i] = money_res[i].replace(re.findall('(<table.*?)<table', money_res[i], re.S)[0], '')
                
            money_num = 'False'
            for i in range(len(money_res)):
                if '报价' in money_res[i] or '金额' in money_res[i]:
                    money_num = i
                    pass
            if money_num != 'False':
#                print ('获取第二表格')
                money_del_str_list = re.findall('<.*?>', money_res[money_num])
                money_del_list = []
                for m_d_str in money_del_str_list:
                    if '<table' not in m_d_str and '<tbody' not in m_d_str and '<tr' not in m_d_str and '<td' not in m_d_str and '</table' not in m_d_str and '</tbody' not in m_d_str and '</tr' not in m_d_str and '</td' not in m_d_str:
                        money_del_list.append(m_d_str)
                money_del_res = money_res[money_num]
                for x in money_del_list:
                    money_del_res = money_del_res.replace(x + '\n', '')
                    money_del_res = money_del_res.replace(x, '')
                new_res = etree.HTML(money_del_res)
                h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
                table = new_res.xpath('//table/tbody/tr/td/text()')
                
#                print (h_tab)
                line_num = math.ceil(len(table)/len(h_tab))
#                print (line_num)
                money_result_dict = {}
                for i in range(int(line_num)):
                    n = i+1
                    line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace(' ', '') for x in line_res]
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    money_result_dict[n] = line_res
                target_num = 'False'
                tag_comp = table_result["中标供应商"].replace('  ', '')
                tag_comp = tag_comp.replace(' ', '')
                for ind, re_list in money_result_dict.items():   
                    if tag_comp in re_list:
                        target_num = ind
                        break
                if target_num != 'False':
                    for i in range(len(money_result_dict[1])):
                        if "报价" in money_result_dict[1][i]:
                            if "万元" in money_result_dict[1][i]:
                                table_result["中标金额"] = money_result_dict[target_num][i] + "万元"
                            else:
                                table_result["中标金额"] = money_result_dict[target_num][i]
    except:
        1
    return table_result



# 获取投标人表格数据
def get_tbr_table(keyword, res, tbr_word_list):
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    table_result = []
    try:
        cut_res = re.findall(keyword + '.*?<table.*?/table>', res, re.S)
        n_res = re.findall('<table.*?/table>', cut_res[0], re.S)
        del_str_list = re.findall('<.*?>', n_res[0])
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<thead' not in d_str and '</thead' not in d_str and '<th' not in d_str and '</th' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str:
                del_list.append(d_str)
        del_res = n_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
#        print (del_res)
        new_res = etree.HTML(del_res)
        try:
            if "<thead" not in del_res:
                del_stat = False
                h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
                if len(h_tab) == 1:
                    h_tab = new_res.xpath('//table/tbody/tr[2]/td/text()')
                    line_num = math.ceil((len(table1)-1)/len(h_tab))
                    del_stat = True
                else:
                    line_num = math.ceil((len(table1))/len(h_tab))
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
    #            print (h_tab)
                
        #        print (line_num)
                result_dict = {}
                for i in range(int(line_num)):
                    if del_stat:
                        n = i+2
                    else:
                        n = i+1
                    line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    if del_stat:
                        result_dict[n-1] = line_res
                    else:
                        result_dict[n] = line_res
    #            print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
    #            print ("投标人表格数据", table_result)
                if len(table_result) == 0:
                    result_dict = {}
                    h_tab = new_res.xpath('//table/tr[1]/td/text()')
                    h_tab = [x.strip('\n') for x in h_tab]
                    h_tab = [x.strip('\t') for x in h_tab]
                    h_tab = [x.replace('\xa0', '') for x in h_tab]
                    h_tab = [x.replace(' ', '') for x in h_tab]
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')
                    table1 = [x.strip('\n') for x in table1]
                    table1 = [x.strip('\t') for x in table1]
                    table1 = [x.replace('\xa0', '') for x in table1]
                    table1 = [x.replace(' ', '') for x in table1]
    #                print ('htab', h_tab)
                    line_num = math.ceil((len(table1))/len(h_tab))
        #            print (line_num)
                    result_dict = {}
                    result_dict[1] = h_tab
                    for i in range(line_num):
                        n = i+1
                        line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                        result_dict[n+1] = line_res
    #                print (result_dict)
                    for tb_word in tbr_word_list:
                        for tab_val in result_dict[1]:
                            if tb_word in tab_val and "排序" not in tab_val:
                                tb_ind = result_dict[1].index(tab_val)
                                for i in range(1, len(result_dict)):
                                    j = i+1
                                    if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                        if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                            table_result.append(result_dict[j][tb_ind-1])
                                    else:
                                        if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                            table_result.append(result_dict[j][tb_ind])
                                break
                        if len(table_result) != 0:
                            break
            else:
#                print ("有thead")
                result_dict = {}
                h_tab = new_res.xpath('//table/thead/tr[1]/th/text()')
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
#                print (h_tab)
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
                line_num = math.ceil((len(table1)+len(h_tab))/len(h_tab))
#                print (table1)
    #            print (line_num)
                result_dict = {}
                result_dict[1] = h_tab
                for i in range(line_num):
                    n = i+1
                    if n == 1:
                        line_res = new_res.xpath('//table/thead/tr[%s]/th/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    else:
                        line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n-1))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n+1] = line_res
#                print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
        except Exception as e:
#            print ('投标人第一次尝试报错', str(e))
            try:
                h_tab = new_res.xpath('//table/tr[1]/td/text()')
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
#                print (h_tab)
                line_num = math.ceil((len(table1))/len(h_tab))
    #            print (line_num)
                result_dict = {}
                for i in range(1):
                    n = i+1
                    line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n] = line_res
#                print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
            except Exception as e:
#                print ("投标人第二次尝试报错", str(e))
                1
    except:
        1
    return table_result




def get_tbr_table_new(keyword, res, tbr_word_list):
#    print (11111)
    res = res.replace('<b>联系方式</b>', '')
    res = res.replace('<b>查看企业资料</b>', '')
    table_result = []
    try:
#        print (2222)
        cut_res = re.findall(keyword + '.*?<table.*?/table>', res, re.S)
#        print (cut_res)
        n_res = re.findall('<table.*?/table>', cut_res[0], re.S)
        del_str_list = re.findall('<.*?>', n_res[0])
        del_list = []
        for d_str in del_str_list:
            if '<table' not in d_str and '<tbody' not in d_str and '<thead' not in d_str and '</thead' not in d_str and '<tr' not in d_str and '<td' not in d_str and '</table' not in d_str and '</tbody' not in d_str and '</tr' not in d_str and '</td' not in d_str and '<th' not in d_str and '</th' not in d_str:
                del_list.append(d_str)
        del_res = n_res[0]
        for x in del_list:
            del_res = del_res.replace(x + '\n', '')
            del_res = del_res.replace(x, '')
        del_res = re.sub("<td.*></td>", "<td> </td>", del_res)
        del_res = re.sub("<th.*></th>", "<th> </th>", del_res)
#        print (del_res)
        new_res = etree.HTML(del_res)
        try:
            if "<thead" not in del_res and '<th' not in del_res:
                del_stat = False
                h_tab = new_res.xpath('//table/tbody/tr[1]/td/text()')
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
                if len(h_tab) == 1:
                    h_tab = new_res.xpath('//table/tbody/tr[2]/td/text()')
                    line_num = math.ceil((len(table1)-1)/len(h_tab))
                    del_stat = True
                else:
                    line_num = math.ceil((len(table1))/len(h_tab))
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
    #            print (h_tab)
                
        #        print (line_num)
                result_dict = {}
                for i in range(int(line_num)):
                    if del_stat:
                        n = i+2
                    else:
                        n = i+1
                    line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    if del_stat:
                        result_dict[n-1] = line_res
                    else:
                        result_dict[n] = line_res
    #            print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
    #            print ("投标人表格数据", table_result)
                if len(table_result) == 0:
                    result_dict = {}
                    h_tab = new_res.xpath('//table/tr[1]/td/text()')
                    h_tab = [x.strip('\n') for x in h_tab]
                    h_tab = [x.strip('\t') for x in h_tab]
                    h_tab = [x.replace('\xa0', '') for x in h_tab]
                    h_tab = [x.replace(' ', '') for x in h_tab]
                    table1 = new_res.xpath('//table/tbody/tr/td/text()')
                    table1 = [x.strip('\n') for x in table1]
                    table1 = [x.strip('\t') for x in table1]
                    table1 = [x.replace('\xa0', '') for x in table1]
                    table1 = [x.replace(' ', '') for x in table1]
    #                print ('htab', h_tab)
                    line_num = math.ceil((len(table1))/len(h_tab))
        #            print (line_num)
                    result_dict = {}
                    result_dict[1] = h_tab
                    for i in range(line_num):
                        n = i+1
                        line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                        result_dict[n+1] = line_res
    #                print (result_dict)
                    for tb_word in tbr_word_list:
                        for tab_val in result_dict[1]:
                            if tb_word in tab_val and "排序" not in tab_val:
                                tb_ind = result_dict[1].index(tab_val)
                                for i in range(1, len(result_dict)):
                                    j = i+1
                                    if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                        if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                            table_result.append(result_dict[j][tb_ind-1])
                                    else:
                                        if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                            table_result.append(result_dict[j][tb_ind])
                                break
                        if len(table_result) != 0:
                            break
            else:
#                print ("有thead")
                result_dict = {}
                h_tab = new_res.xpath('//table/thead/tr[1]/th/text()')
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
#                print (h_tab)
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
                line_num = math.ceil((len(table1)+len(h_tab))/len(h_tab))
#                print (table1)
#    #            print (line_num)
                result_dict = {}
                result_dict[1] = h_tab
                for i in range(line_num):
                    n = i+1
                    if n == 1:
                        line_res = new_res.xpath('//table/thead/tr[%s]/th/text()' % str(n))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    else:
                        line_res = new_res.xpath('//table/tbody/tr[%s]/td/text()' % str(n-1))
                        line_res = [x.replace('\t', '') for x in line_res]
                        line_res = [x.replace('\n', '') for x in line_res]
                        line_res = [x.replace('\r', '') for x in line_res]
                        line_res = [x.replace('\\t', '') for x in line_res]
                        line_res = [x.replace('\\n', '') for x in line_res]
                        line_res = [x.replace('\\r', '') for x in line_res]
                        line_res = [x.replace('\xa0', '') for x in line_res]
                        line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n+1] = line_res
#                print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
        except Exception as e:
#            print ('投标人第一次尝试报错', str(e))
            try:
                h_tab = new_res.xpath('//table/tr[1]/td/text()')
                h_tab = [x.strip('\n') for x in h_tab]
                h_tab = [x.strip('\t') for x in h_tab]
                h_tab = [x.replace('\xa0', '') for x in h_tab]
                h_tab = [x.replace(' ', '') for x in h_tab]
                table1 = new_res.xpath('//table/tbody/tr/td/text()')
                table1 = [x.strip('\n') for x in table1]
                table1 = [x.strip('\t') for x in table1]
                table1 = [x.replace('\xa0', '') for x in table1]
                table1 = [x.replace(' ', '') for x in table1]
#                print (h_tab)
                line_num = math.ceil((len(table1))/len(h_tab))
    #            print (line_num)
                result_dict = {}
                for i in range(1):
                    n = i+1
                    line_res = new_res.xpath('//table/tbody[1]/tr[%s]/td/text()' % str(n))
                    line_res = [x.replace('\t', '') for x in line_res]
                    line_res = [x.replace('\n', '') for x in line_res]
                    line_res = [x.replace('\r', '') for x in line_res]
                    line_res = [x.replace('\\t', '') for x in line_res]
                    line_res = [x.replace('\\n', '') for x in line_res]
                    line_res = [x.replace('\\r', '') for x in line_res]
                    line_res = [x.replace('\xa0', '') for x in line_res]
                    line_res = [x.replace(' ', '') for x in line_res]
                    result_dict[n] = line_res
#                print (result_dict)
                for tb_word in tbr_word_list:
                    for tab_val in result_dict[1]:
                        if tb_word in tab_val and "排序" not in tab_val:
                            tb_ind = result_dict[1].index(tab_val)
                            for i in range(1, len(result_dict)):
                                j = i+1
                                if len(result_dict[j]) < len(result_dict[1]) and tb_ind-1 >= 0:
                                    if "：" not in result_dict[j][tb_ind-1] and ":" not in result_dict[j][tb_ind-1]:
                                        table_result.append(result_dict[j][tb_ind-1])
                                else:
                                    if "：" not in result_dict[j][tb_ind] and ":" not in result_dict[j][tb_ind]:
                                        table_result.append(result_dict[j][tb_ind])
                            break
                    if len(table_result) != 0:
                        break
            except Exception as e:
#                print ("投标人第二次尝试报错", str(e))
                1
    except:
        1
    return table_result